import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydaq -o ydaq.json
https://992kp15.992kp524.work:8443/index.html
'''


class YdaqSpider(scrapy.Spider):
    name = "ydaq"
    # 前缀
    prefix = 'https://992kp15.'
    # 中缀
    website = '992kp524'
    # 后缀
    suffix = '.work:8443/'
    allowed_domains = [website + '.work:8443']
    start_urls = [prefix + website + suffix + "index.html"]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_urls = re.findall(r'\(.*?\.M3U8', content, re.IGNORECASE)
        picture_urls = re.findall(r'get_img_url.*?"\),', content, re.IGNORECASE)
        tags = response.xpath("//div[@class='ttss1 navv_obxobx position-indicator']//a/text()").extract()
        if len(video_urls) and len(picture_urls) and len(tags):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tags[1], url=response.url, name=tags[-1], pUrl=split_joint('https://kp-i3932.com/', picture_urls[0].split('"')[1]), vUrl=split_joint('https://kp-p2805.com/', video_urls[0].split('"')[1]))
        url_list = get_url(content)
        # 提取url
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)